package hu.cskassai.sad.gui.panels;

import hu.cskassai.sad.business.GraphManager;
import hu.cskassai.sad.business.vo.HeuristicPropertyVO;
import hu.cskassai.sad.model.graph.Edge;
import hu.cskassai.sad.model.graph.Vertex2D;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class AddHeuristicDialog extends JDialog {
	
	private final JPanel contentPanel = new JPanel();
	private final JTextField pathField;
	private final JTextField fullNameField;
	private final JTextField nameField;
	
	private boolean succeeded;
	
	public boolean isSucceeded() {
		return succeeded;
	}
	
	/**
	 * Create the dialog.
	 */
	public AddHeuristicDialog(final JFrame jFrame, final JComboBox jComboBox,
		final GraphManager<Vertex2D, Edge> graphManager)
	{
		super(jFrame);
		setTitle("New heuristic");
		setBounds(100, 100, 586, 269);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		pathField = new JTextField();
		pathField.setBounds(184, 53, 226, 19);
		contentPanel.add(pathField);
		pathField.setColumns(10);
		
		final JButton browseButton = new JButton("Browse");
		browseButton.setBounds(422, 50, 117, 25);
		browseButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				final JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				final int returnVal = fc.showDialog(fc, "Open");
				try {
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						final File file = fc.getSelectedFile();
						pathField.setText(file.getAbsolutePath());
						
					}
					
				} catch (final Exception ex) {
					
				}
			}
		});
		contentPanel.add(browseButton);
		
		fullNameField = new JTextField();
		fullNameField.setBounds(184, 94, 287, 19);
		contentPanel.add(fullNameField);
		fullNameField.setColumns(10);
		
		nameField = new JTextField();
		nameField.setBounds(184, 137, 114, 19);
		contentPanel.add(nameField);
		nameField.setColumns(10);
		
		final JLabel lblClassFilePath = new JLabel("Class file path");
		lblClassFilePath.setBounds(12, 55, 154, 15);
		contentPanel.add(lblClassFilePath);
		
		final JLabel lblFullyQualifiedName = new JLabel("Fully qualified name:");
		lblFullyQualifiedName.setBounds(12, 96, 160, 15);
		contentPanel.add(lblFullyQualifiedName);
		
		final JLabel lblName = new JLabel("Name:");
		lblName.setBounds(12, 139, 70, 15);
		contentPanel.add(lblName);
		{
			final JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				final JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(final ActionEvent e) {
						final String url = pathField.getText();
						final String fullName = fullNameField.getText();
						final String name = nameField.getText();
						final HeuristicPropertyVO heuristicPropertyVO = new HeuristicPropertyVO(url, fullName, name);
						try {
							graphManager.addHeuristic(heuristicPropertyVO);
							jComboBox.addItem(name);
							succeeded = true;
							dispose();
						} catch (final Exception exception) {
							exception.printStackTrace();
							
						}
					}
					
				});
				
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				final JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(final ActionEvent e) {
						dispose();
					}
				});
				
				buttonPane.add(cancelButton);
			}
		}
	}
	
}
